package coreSimulation;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.Border;

public class QueueUI extends JFrame implements Observer {

	private Queue queue;
	private JPanel graphicPanel;
	private JButton buildQueueButton;
	JScrollPane scrollPane;

	public QueueUI(Queue queue) {
		this.queue = queue;
		queue.addObserver(this);

		// ensure program ends when window closes
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(540, 920);
		setLocation(10, 20);

		// add button panel and result field to the content pane
		Container contentPane = getContentPane();
		contentPane.add(createControlPanel(), BorderLayout.NORTH);

		graphicPanel = new JPanel(new FlowLayout());
		graphicPanel.setBackground(Color.WHITE);

		scrollPane = new JScrollPane(graphicPanel);
		scrollPane.setPreferredSize(new Dimension(520, 900));
		scrollPane
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

		contentPane.add(scrollPane, BorderLayout.CENTER);

		setVisible(true);

	}

	@Override
	public void update(Observable arg0, Object arg1) {

		updateGraphicPanel();

	}

	private JPanel createControlPanel() {
		// north panel shows the button to start processing
		buildQueueButton = new JButton("Begin simulation");
		JPanel controlPanel = new JPanel();
		controlPanel.add(buildQueueButton);
		return controlPanel;

	}

	private void updateGraphicPanel() {
		// add a label for each passenger in the queue colouring the next
		// passenger in the queue yellow


		Color color = Color.YELLOW;

		graphicPanel.removeAll();

		int numberOfPassengers = 0;
		for (Passenger passenger : queue) {
			String labelText = formatPassengerText(passenger);
			JLabel label = new JLabel(labelText);
			label.setPreferredSize(new Dimension(500, 30));
			label.setBackground(color);
			label.setOpaque(true);
			label.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
			color = Color.LIGHT_GRAY;
			Border paddingBorder = BorderFactory.createEmptyBorder(10, 10, 10,
					10);

			Border border = BorderFactory.createLineBorder(Color.GRAY);

			label.setBorder(BorderFactory.createCompoundBorder(border,
					paddingBorder));
			graphicPanel.add(label);
			numberOfPassengers++;
		}

		// grow/shrink this panel to accommodate the number of passengers
		// currently in the queue
		graphicPanel.setPreferredSize(new Dimension(500,
				(numberOfPassengers * 35) + 10));

		revalidate();
		repaint();

		try {
			Thread.sleep(25);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}

	private String formatPassengerText(Passenger passenger) {
		Baggage bag = passenger.getBaggage();

		String bagWeight = Double.toString(bag.getWeight());
		String bagWeight1 = bagWeight.substring(bagWeight.indexOf("."),
				bagWeight.length());
		if (bagWeight1.compareTo(".0") == 0)
			bagWeight1 = "";
		bagWeight1 += "kg";
		bagWeight = bagWeight.substring(0, bagWeight.indexOf("."));

		String format = "%1$-15s%2$-25s%3$-4sx  %4$-4sx  %5$-7s%6$3s";
		String labelText = String.format(format, passenger.getBookingRef(),
				passenger.getName(), bag.getWidth(), bag.getDepth(),
				bag.getHeight(), bagWeight + bagWeight1);
		return labelText;
	}

	private JPanel createTextPanel() {
		JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));

		return panel;
	}

	public void addbuildQueueButtonListener(ActionListener al) {
		buildQueueButton.addActionListener(al);
	}
}
